package app.goods.info.views;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.apache.commons.lang3.StringUtils;

import com.formdev.flatlaf.FlatLightLaf;

import app.goods.info.services.LoginTokenService;
import app.goods.info.services.domain.LoginSmsUserInfo;
import app.goods.info.utils.CommonUtils;
import app.goods.info.utils.InterfaceUtils;
import app.goods.info.views.listeners.ButtonActionPerformListenner;
import cn.hutool.json.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LoginDialog extends JDialog {

	private static final Logger LOGGER = LoggerFactory.getLogger(LoginDialog.class);
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTextField userName;
	private JPasswordField passwordField;
	private int loginType;
	private LoginDialog loginDialog;

	private JTextField mobileTextField;
	private JTextField verifyCodeTextField;
	private String captchaToken;
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UIManager.put("Component.focusWidth", 1);
			UIManager.put("Component.innerFocusWidth", 1);
			UIManager.put("TabbedPane.showTabSeparators", true);
			UIManager.put("Component.arc", 50);
			UIManager.put("Table.showHorizontalLines", true);
			UIManager.put("Table.showVerticalLines", true);
			UIManager.put("Table.cellNoFocusBorder", 2);

			FlatLightLaf.setup();
//			if (CommonUtils.isExpired()) {
//				InterfaceUtils.createMessageBox("软件遇到致命错误，已崩溃。。。呵呵。。。。");
//				System.exit(0);
//			}
			LoginDialog dialog = new LoginDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setLocationRelativeTo(null);
			dialog.setVisible(true);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
	}

	/**
	 * Create the dialog.
	 */
	public LoginDialog() {
		loginDialog = this;
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setAlwaysOnTop(true);
		setModal(true);
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JTabbedPane jTabbedPane = new JTabbedPane();
			JPanel loginBySmsPanel = new JPanel();
			loginBySmsPanel.setLayout(null);
			JLabel mobileJLabel = new JLabel("手机号：");
			mobileJLabel.setBounds(87, 64, 61, 16);
			mobileTextField = new JTextField();
			mobileTextField.setBounds(147, 59, 130, 26);
			mobileTextField.setText("17834950563");
			loginBySmsPanel.add(mobileTextField);
			mobileTextField.setColumns(10);
			loginBySmsPanel.add(mobileJLabel);
			JLabel captchCodeJLabel = new JLabel("验证码：");
			captchCodeJLabel.setBounds(87, 102, 61, 16);
			loginBySmsPanel.add(captchCodeJLabel);
			JButton smsCodeButton = new JButton("获取短信验证码");
			smsCodeButton.setBounds(287, 59, 130, 26);
			smsCodeButton.addActionListener(e -> {
				LoginTokenService loginTokenService = new LoginTokenService();
				LoginSmsUserInfo loginSmsUserInfo = loginTokenService.buildLoginBySmsUserInfo(mobileTextField.getText(),
						this.getCaptchaToken(), verifyCodeTextField.getText());
				String smsCodeRes = loginTokenService.getSmsCodeResponse(loginSmsUserInfo);
				System.out.println("smsCodeRes===" + smsCodeRes);
				if ("0".equals(JSONUtil.parseObj(smsCodeRes).getStr("code"))) {
					String token = JSONUtil.parseObj(smsCodeRes).getStr("result");
					if (StringUtils.isNotEmpty(token)) {
						loginDialog.setCaptchaToken(token);
					}
				}

			});
			loginBySmsPanel.add(smsCodeButton);
			verifyCodeTextField = new JTextField();
			verifyCodeTextField.setBounds(147, 102, 130, 26);
			verifyCodeTextField.setText("");
			loginBySmsPanel.add(verifyCodeTextField);

			jTabbedPane.addTab("手机验证码登录", loginBySmsPanel);

			JPanel loginByNamePanel = new JPanel();
			loginByNamePanel.setLayout(null);
			JLabel lblNewLabel = new JLabel("用户名：");
			lblNewLabel.setBounds(87, 64, 61, 16);
			loginByNamePanel.add(lblNewLabel);
			JLabel lblNewLabel_1 = new JLabel("密码：");
			lblNewLabel_1.setBounds(87, 102, 61, 16);
			loginByNamePanel.add(lblNewLabel_1);
			userName = new JTextField();
			userName.setBounds(187, 59, 130, 26);
			userName.setText("17834950563");
			loginByNamePanel.add(userName);
			userName.setColumns(10);
			passwordField = new JPasswordField();
			passwordField.setBounds(187, 102, 130, 26);
//			passwordField.setText("Hyl198555@");
			passwordField.setText("Admin_123");
			loginByNamePanel.add(passwordField);

			jTabbedPane.addTab("用户名密码登录", loginByNamePanel);

			jTabbedPane.addChangeListener(new ChangeListener() {
				@Override
				public void stateChanged(ChangeEvent e) {
					int selectedIndex = jTabbedPane.getSelectedIndex();
					loginDialog.setLoginType(selectedIndex);
				}
			});

			contentPanel.add(jTabbedPane, BorderLayout.CENTER);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
				okButton.addActionListener(new ButtonActionPerformListenner(this));
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				cancelButton.addActionListener(new ButtonActionPerformListenner(this));
				buttonPane.add(cancelButton);
			}
		}
	}

	public JTextField getUserName() {
		return userName;
	}

	public void setUserName(JTextField userName) {
		this.userName = userName;
	}

	public JPasswordField getPasswordField() {
		return passwordField;
	}

	public void setPasswordField(JPasswordField passwordField) {
		this.passwordField = passwordField;
	}

	public int getLoginType() {
		return loginType;
	}

	public void setLoginType(int loginType) {
		this.loginType = loginType;
	}

	public JTextField getMobileTextField() {
		return mobileTextField;
	}

	public void setMobileTextField(JTextField mobileTextField) {
		this.mobileTextField = mobileTextField;
	}

	public JTextField getVerifyCodeTextField() {
		return verifyCodeTextField;
	}

	public void setVerifyCodeTextField(JTextField verifyCodeTextField) {
		this.verifyCodeTextField = verifyCodeTextField;
	}

	public String getCaptchaToken() {
		return captchaToken;
	}

	public void setCaptchaToken(String captchaToken) {
		this.captchaToken = captchaToken;
	}

}
